/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.dolphinscheduler.plugin.task.api;

import org.apache.dolphinscheduler.plugin.task.api.utils.OSUtils;

import org.apache.commons.lang3.SystemUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import lombok.NonNull;

public final class ProcessUtils {
  private static final Logger logger = LoggerFactory.getLogger(ProcessUtils.class);

  private ProcessUtils() {
    throw new IllegalStateException("Utility class");
  }

  /**
   * Initialization regularization, solve the problem of pre-compilation performance,
   * avoid the thread safety problem of multi-thread operation
   */
  private static final Pattern MACPATTERN = Pattern.compile("-[+|-]-\\s(\\d+)");

  /**
   * Expression of PID recognition in Windows scene
   */
  private static final Pattern WINDOWSATTERN = Pattern.compile("(\\d+)");

  /**
   * kill tasks according to different task types.
   */
  public static boolean kill(@NonNull TaskExecutionContext request) {
    try {
      logger.info("Begin kill task instance, processId: {}", request.getProcessId());
      int processId = request.getProcessId();
      if (processId == 0) {
        logger.error("Task instance kill failed, processId is not exist");
        return false;
      }

      String cmd = String.format("kill -9 %s", getPidsStr(processId));
      cmd = OSUtils.getSudoCmd(request.getTenantCode(), cmd);
      logger.info("process id:{}, cmd:{}", processId, cmd);

      OSUtils.exeCmd(cmd);
      logger.info("Success kill task instance, processId: {}", request.getProcessId());
      return true;
    } catch (Exception e) {
      logger.error("Kill task instance error, processId: {}", request.getProcessId(), e);
      return false;
    }
  }

  /**
   * get pids str.
   *
   * @param processId process id
   * @return pids pid String
   * @throws Exception exception
   */
  public static String getPidsStr(int processId) throws Exception {
    StringBuilder sb = new StringBuilder();
    Matcher mat = null;
    // pstree pid get sub pids
    if (SystemUtils.IS_OS_MAC) {
      String pids = OSUtils.exeCmd(String.format("%s -sp %d", TaskConstants.PSTREE, processId));
      if (null != pids) {
        mat = MACPATTERN.matcher(pids);
      }
    } else {
      String pids = OSUtils.exeCmd(String.format("%s -p %d", TaskConstants.PSTREE, processId));
      mat = WINDOWSATTERN.matcher(pids);
    }

    if (null != mat) {
      while (mat.find()) {
        sb.append(mat.group(1)).append(" ");
      }
    }

    return sb.toString().trim();
  }

}
